package com.tongji.housediy.view
{
	
	import caurina.transitions.Tweener;
	
	import com.tongji.housediy.events.commonEvent.ChangeData3D;
	import com.tongji.housediy.events.commonEvent.ExchangeDataEvent;
	import com.tongji.housediy.events.commonEvent.GridContainerEvent;
	import com.tongji.housediy.events.commonEvent.MoveCamera3D;
	import com.tongji.housediy.events.commonEvent.ShowHideControlPanelEvent;
	import com.tongji.housediy.model.ExchangeDataModel;
	import com.tongji.housediy.model.ListTypeModel;
	import com.tongji.housediy.model.ViewControlModel;
	import com.tongji.housediy.view.component3D.CreateRoom3D;
	import com.tongji.housediy.vo.Goods;
	
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.controls.Alert;
	import mx.core.IUIComponent;
	import mx.events.ColorPickerEvent;
	import mx.events.DragEvent;
	import mx.managers.DragManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 * 
	 * @author xxj
	 * 
	 */	
	public class Room3DMediator extends Mediator
	{
		[Inject]
		public var view:Room3D;
		
		[Inject]
		public var exchangeDataModel:ExchangeDataModel;
		
		[Inject]
		public var listTypeModel:ListTypeModel;
		[Inject]
		public var viewControlModel:ViewControlModel;
		
		private var scene:CreateRoom3D = new CreateRoom3D();
		private var is3D:Boolean = false;
		
		override public function onRegister():void
		{
			addRoom();
			is3D = true;
			this.addContextListener(ChangeData3D.CHANGE_DATA, addRoom, ChangeData3D);
			this.addContextListener(ChangeData3D.REMOVE_DATA, removeRoom, ChangeData3D);
			this.addContextListener(ChangeData3D.ADD_ANGLE, addAngel, ChangeData3D);
			this.addContextListener(ChangeData3D.SUB_ANGLE, subAngel, ChangeData3D);
			this.addContextListener(ChangeData3D.DOWN_VIEW, downView, ChangeData3D);
			this.addContextListener(ChangeData3D.UP_VIEW, upView, ChangeData3D);
			this.addContextListener(ChangeData3D.RIGHT_VIEW, rightView, ChangeData3D);
			this.addContextListener(ChangeData3D.LEFT_VIEW, leftView, ChangeData3D);
			this.addContextListener(ChangeData3D.BACK_VIEW, backView, ChangeData3D);
			this.addContextListener(ChangeData3D.FRONT_VIEW, frontView, ChangeData3D);
			this.addContextListener(ChangeData3D.MOVE_DOWN, moveDown, ChangeData3D);
			this.addContextListener(ChangeData3D.MOVE_UP, moveUp, ChangeData3D);
			this.addContextListener(ChangeData3D.MOVE_LEFT, moveLeft, ChangeData3D);
			this.addContextListener(ChangeData3D.MOVE_RIGHT, moveRight, ChangeData3D);
			this.addContextListener(ChangeData3D.MOVE_STOP, moveStop, ChangeData3D);
			this.addContextListener(ChangeData3D.MOVE_CENTER, holdAngel, ChangeData3D);
			this.addContextListener(ChangeData3D.CAMERA_DOWN, cameraDown, ChangeData3D);
			this.addContextListener(ChangeData3D.CAMERA_UP, cameraUp, ChangeData3D);
			this.addContextListener(ChangeData3D.PROJECTION_CHANGE, projectionChange, ChangeData3D);
			
			this.view.addEventListener(ExchangeDataEvent.UPDATE_MAP_POSITION_ROTAION,change2Dposition);
			
			this.addContextListener(ExchangeDataEvent.CHANGE_WANDER_VIEW, changeFreeView, ExchangeDataEvent);
			this.addContextListener(MoveCamera3D.CAMERA_NEAR, cameraNear, MoveCamera3D);
			this.addContextListener(MoveCamera3D.CAMERA_FAR, cameraFar, MoveCamera3D);
			this.addContextListener(MoveCamera3D.CAMERA_SELECT, cameraSelect, MoveCamera3D);
			this.view.addEventListener(MouseEvent.MOUSE_WHEEL,cameraMove);
			
			this.addContextListener(ColorPickerEvent.CHANGE, resetWallMaterial);
			
			view.rawChildren.addChild(scene);
			/*view.xHSlider.addEventListener(Event.CHANGE, resetWallMaterial);
			view.yHSlider.addEventListener(Event.CHANGE, resetWallMaterial);
			view.rangeHSlider.addEventListener(Event.CHANGE, resetWallMaterial);
			view.wholeColorPicker.addEventListener(ColorPickerEvent.CHANGE, resetWallMaterial);
			view.cornerColorPicker.addEventListener(ColorPickerEvent.CHANGE, resetWallMaterial);
			view.xHSlider1.addEventListener(Event.CHANGE, resetCeilingMaterial);
			view.yHSlider1.addEventListener(Event.CHANGE, resetCeilingMaterial);
			view.rangeHSlider1.addEventListener(Event.CHANGE, resetCeilingMaterial);
			view.wholeColorPicker1.addEventListener(ColorPickerEvent.CHANGE, resetCeilingMaterial);
			view.cornerColorPicker1.addEventListener(ColorPickerEvent.CHANGE, resetCeilingMaterial);*/
			
			
			this.addContextListener(ShowHideControlPanelEvent.SHOW_CONTROL_PANEL, showControlPanelHandler, ShowHideControlPanelEvent);
			this.addContextListener(ShowHideControlPanelEvent.HIDE_CONTROL_PANEL, hideControlPanelHandler, ShowHideControlPanelEvent);
			
			// 侦听墙面的拖拽事件
			view.addEventListener(DragEvent.DRAG_ENTER, dragEnterInRoomHandler);
			view.addEventListener(DragEvent.DRAG_DROP, dragDropInRoomHandler);
		}
		
		public function addRoom(event:Event = null):void
		{
			if (!is3D)
			{
				var exchangeDataLength:int = exchangeDataModel.relatedExchangeDataVector.length;
				scene.insidePosition2D = new Vector.<Number>;
				scene.outsidePosition2D = new Vector.<Number>;
				for (var i:int = 0; i < exchangeDataLength; i++)
				{
					scene.roomCount = i;
					scene.addRoom(exchangeDataModel.relatedExchangeDataVector[i].vertices,
					exchangeDataModel.relatedExchangeDataVector[i].curveVector,
					exchangeDataModel.relatedExchangeDataVector[i].initialWallVector,
					exchangeDataModel.relatedExchangeDataVector[i].borderChildVector,
					exchangeDataModel.relatedExchangeDataVector[i].relatedFloor,
					exchangeDataModel.relatedExchangeDataVector[i].roomHeight / 10,
					exchangeDataModel.relatedExchangeDataVector[i].doorWindowRelatedMap,
					exchangeDataModel.relatedExchangeDataVector[i].borderVector)
				}
				scene.addLight();
				scene.distanceScale = Math.pow(1/1.1, viewControlModel.dimensionValue - 5);
				//scene.setCameraPosition(exchangeDataModel.cameraPosition, exchangeDataModel.cameraRotation)
				if (scene.inStage)
				{
					scene.initEightView();
				}
				is3D = true;
			}
		}
		
		public function removeRoom(event:Event = null):void
		{
			if (is3D)
			{
				scene.removeRoom();
				is3D = false;
			}
		}
		
		private function addAngel(event:Event = null):void
		{
			if (scene.inStage && scene.state == "eightView")
			{
				scene.changeEightView(-1);
			}
		}
		
		private function holdAngel(event:Event = null):void
		{
			if (scene.inStage)
			{
				scene.camera.view.x = 0;
				scene.camera.view.y = 0;
				scene.changeEightView(0);
			}
		}
		
		private function subAngel(event:Event = null):void
		{
			if (scene.inStage && scene.state == "eightView")
			{
				scene.changeEightView(1);
			}
		}
		
		private function downView(event:Event = null):void
		{
			if (scene.inStage)
			{
				scene.changeSixView(-1);
			}
		}
		
		private function upView(event:Event = null):void
		{
			if (scene.inStage)
			{
				scene.changeSixView(0);
			}
		}
		
		private function rightView(event:Event = null):void
		{
			if (scene.inStage)
			{
				scene.changeSixView(1);
			}
		}
		
		private function backView(event:Event = null):void
		{
			if (scene.inStage)
			{
				scene.changeSixView(2);
			}
		}
		
		private function leftView(event:Event = null):void
		{
			if (scene.inStage)
			{
				scene.changeSixView(3);
			}
		}
		
		private function frontView(event:Event = null):void
		{
			if (scene.inStage)
			{
				scene.changeSixView(4);
			}
		}
		
		private function moveDown(event:Event = null):void
		{
			if (scene.inStage && scene.state == "eightView")
			{
				scene.moveSpeed = 3;
				scene.direction = 2;
			}
		}
		
		private function moveUp(event:Event = null):void
		{
			if (scene.inStage && scene.state == "eightView")
			{
				scene.moveSpeed = 3;
				scene.direction = 0;
			}
		}
		
		private function moveRight(event:Event = null):void
		{
			if (scene.inStage && scene.state == "eightView")
			{
				scene.moveSpeed = 3;
				scene.direction = 3;
			}
		}
		
		private function moveLeft(event:Event = null):void
		{
			if (scene.inStage && scene.state == "eightView")
			{
				scene.moveSpeed = 3;
				scene.direction = 1;
			}
		}
		
		private function moveStop(event:Event = null):void
		{
			if (scene.inStage && scene.state == "eightView")
			{
				scene.moveSpeed = 0;
				scene.rotateSpeed = 0;
			}
		}
		
		private function cameraDown(event:Event = null):void
		{
			if (scene.inStage && scene.state == "eightView")
			{
				scene.rotateSpeed = -1;
			}
		}
		
		private function cameraUp(event:Event = null):void
		{
			if (scene.inStage && scene.state == "eightView")
			{
				scene.rotateSpeed = 1;
			}
		}
		
		private function changeFreeView(event:ExchangeDataEvent):void
		{
			if (scene.inStage)
			{
				scene.setCameraPosition(event.position, event.rotation);
			}
		}
		
		private function change2Dposition(event:ExchangeDataEvent):void
		{
			this.dispatch(new ExchangeDataEvent(ExchangeDataEvent.UPDATE_MAP_POSITION_ROTAION, event.position, event.rotation))
		}
		
		private function projectionChange(event:Event = null):void
		{
			if (scene.inStage)
			{
				scene.projectionChange();
			}
		}
		
		private function cameraNear(event:Event = null):void
		{
			if (scene.inStage && scene.state == "eightView")
			{
				scene.distanceScale = scene.distanceScale / 1.1
				scene.changeEightView(0);
			}
		}
		
		private function cameraFar(event:Event = null):void
		{
			if (scene.inStage && scene.state == "eightView")
			{
				scene.distanceScale = scene.distanceScale * 1.1
				scene.changeEightView(0);
			}
		}
		
		private function cameraSelect(event:MoveCamera3D = null):void
		{
			if (scene.inStage && scene.state == "eightView")
			{
				if (event.scaleMultiple < 0)
					scene.distanceScale = Math.pow(1.1, -event.scaleMultiple);
				else
					scene.distanceScale = Math.pow(1/1.1, event.scaleMultiple);
				scene.changeEightView(0);
			}
		}
		
		private function cameraMove(e:MouseEvent):void
		{
			if (scene.inStage && scene.state == "eightView")
			{
				if (e.delta < 0 && scene.distanceScale < Math.pow(1.1, 4))
				{
					scene.distanceScale = scene.distanceScale * 1.1
					dispatch(new GridContainerEvent(GridContainerEvent.UPDATE_SCROLL_VALUE, -1));
				}
				else if (e.delta > 0 && scene.distanceScale > Math.pow(1/1.1, 4))
				{
					scene.distanceScale = scene.distanceScale / 1.1
					dispatch(new GridContainerEvent(GridContainerEvent.UPDATE_SCROLL_VALUE, 1));
				}
				scene.changeEightView(0);
			}
		}
		
		public function resetWallMaterial(event:ColorPickerEvent):void
		{
			if (scene.selectedWall == null)
				Alert.show("请先双击选择需要更改的墙面");
			else
			{
				if (listTypeModel.wallLevel1XMLList.length == 3)
				{
					scene.resetSelectedBitmap(new BitmapData(2, 2, false, event.color), null);
					exchangeDataModel.relatedExchangeDataVector[scene.selectedWall.roomCount].borderVector[scene.selectedWall.wallCount].changeSizeAndColor(null, scene.selectedWall.wallBitmap, null);
				}
				else
				{
					scene.resetSelectedBitmap(null, new BitmapData(2, 2, false, event.color));
					exchangeDataModel.relatedExchangeDataVector[scene.selectedWall.roomCount].borderVector[scene.selectedWall.wallCount].changeSkirtingSizeAndColor(null, scene.selectedWall.underBitmap, null);
				}
			}
		}
		
		
		private function dragEnterInRoomHandler(event:DragEvent):void
		{
			if (event.dragSource.formats[0] == "mapFormat")
			{
				DragManager.acceptDragDrop(IUIComponent(event.target));
			}
		}
		
		private function dragDropInRoomHandler(event:DragEvent):void
		{
			if (scene.selectedWall == null)
				Alert.show("请先双击选择需要更改的墙面");
			else
			{
				if (listTypeModel.wallLevel1XMLList.length == 3)
				{
					scene.resetSelectedBitmap(event.dragSource.dataForFormat(event.dragSource.formats[0])[0] as BitmapData, null);
					exchangeDataModel.relatedExchangeDataVector[scene.selectedWall.roomCount].borderVector[scene.selectedWall.wallCount].changeSizeAndColor(null, scene.selectedWall.wallBitmap, event.dragSource.dataForFormat(event.dragSource.formats[0])[1] as Goods);
				}
				else
				{
					scene.resetSelectedBitmap(null, event.dragSource.dataForFormat(event.dragSource.formats[0])[0] as BitmapData);
					exchangeDataModel.relatedExchangeDataVector[scene.selectedWall.roomCount].borderVector[scene.selectedWall.wallCount].changeSkirtingSizeAndColor(null, scene.selectedWall.underBitmap, event.dragSource.dataForFormat(event.dragSource.formats[0])[1] as Goods);
				}
			}
		}
		
		/*
		public function resetCeilingMaterial(event:Event = null):void
		{
			scene.resetSelectedMaterial( new Point(128 - view.xHSlider1.value, 128 - view.yHSlider1.value), view.rangeHSlider1.value, view.wholeColorPicker1.selectedColor, view.cornerColorPicker1.selectedColor);
		}
		*/
		
		private function showControlPanelHandler(event:ShowHideControlPanelEvent):void
		{
			view.width = view.stage.stageWidth - 272;
			scene.stageResizeHandler();
		}
		
		private function hideControlPanelHandler(event:ShowHideControlPanelEvent):void
		{
			view.width = view.stage.stageWidth - 10;
			scene.stageResizeHandler();
		}
	}
}